/*
 * Application.c
 *
 *  Created on: Mar 12, 2012
 *      Author: mikaelgra
 */

#include <stdlib.h>

#include "Application.h"
#include "AlarmManager.h"
#include "ConsoleManager.h"
#include "Alarm.h"
#include "gpio/Define.h"

/**
 * Boolean indicating whether or not the Application
 * is currently running.
 */
bool_t IsRunning;

/**
 * Main function. Arguments are the command-line parameters.
 * These are unused.
 *
 * @param argc
 * 		Is the number of separate words in the argv argument
 * @param argv
 * 		Is the words passed as arugments through the commandline
 *
 * @return
 * 		Is a code signifying whether or not the program shutdown
 * 		correctly.
 */
int main(int argc, char** argv)
{
	Application_Initialize();
	Application_Start();
	Application_Finalize();
	return 0;
}

/**
 * Initializes the Application module, which is
 * responsible for initializing both the
 * AlarmManager and ConsoleManager.
 */
void Application_Initialize()
{
	AlarmManager_Initialize();
	ConsoleManager_Initialize();

	// Add alarms
	struct stAlarm_t *alarm1 = Alarm_New(
			"Kitchen Alarm", 				// name
			"THE CAKE IS IN DANGER!!!", 	// twitter message
			"42",							// password
			60, 							// trigger time
			0, 								// trigger value
			4,								// gpio input
			7);								// gpio output

	AlarmManager_AddAlarm(alarm1);

	IsRunning = FALSE;
}


/**
 * Starts the Application responsible for the main logic.
 */
void Application_Start()
{
	IsRunning = TRUE;
	while(IsRunning)
	{
		// wait for an alarm
		struct stAlarm_t* alarm = NULL;
		while(alarm == NULL)
		{
			alarm = AlarmManager_CheckAlarms();
			sleep(1);
		}

		ConsoleManager_PromptForPassword(alarm);
	}
}

/**
 * Finalizes the Application finalizing both the
 * AlarmManager and the ConsoleManager.
 */
void Application_Finalize()
{
	AlarmManager_Finalize();
	ConsoleManager_Finalize();
}
